package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.StockIncomeBill;
import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.domain.StockOutcomebillItem;
import cn.wolfcode.wms.exception.LogicException;
import cn.wolfcode.wms.mapper.StockOutcomeBillMapper;
import cn.wolfcode.wms.mapper.StockOutcomebillItemMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import cn.wolfcode.wms.service.IStockOutcomeBillService;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class StockOutcomeService implements IStockOutcomeBillService {



    @Autowired
    private StockOutcomeBillMapper stockoutcomeebillMapper;
    @Autowired
    private StockOutcomebillItemMapper stockoutcomeebillItemMapper;
    @Autowired
    private IProductStockService productStockService;



    public int deleteByPrimaryKey(Long id) {
        return stockoutcomeebillMapper.deleteByPrimaryKey(id);
    }

    public int insert(StockOutcomeBill record) {
        return stockoutcomeebillMapper.insert(record);
    }

    public StockOutcomeBill selectByPrimaryKey(Long id) {
        return stockoutcomeebillMapper.selectByPrimaryKey(id);
    }

    public List<StockOutcomeBill> selectAll() {
        return stockoutcomeebillMapper.selectAll();
    }

    public int updateByPrimaryKey(StockOutcomeBill record) {
        return stockoutcomeebillMapper.updateByPrimaryKey(record);
    }


    public void saveorupdate(StockOutcomeBill stockoutcomeebill) {

        if (stockoutcomeebill.getId() != null) {
            update(stockoutcomeebill);
        } else {
            save(stockoutcomeebill);
        }
    }

    //具体的更新逻辑
    private void update(StockOutcomeBill bill) {
        //判断状态不为已审核
        StockOutcomeBill bill1 = stockoutcomeebillMapper.selectByPrimaryKey(bill.getId());
        if(bill1.getStatus() == StockIncomeBill.STATUS_AUDITED){
            throw  new LogicException("订单已审核不可修改");
        }
        //1.获取当前用户,设置录入的相关信息
        bill.setInputTime(new Date());
        bill.setInputUser(UserContext.getCurrentUser());
        //2.重置订单状态
        bill.setStatus(StockOutcomeBill.STATUS_NORMAL);
        //3.计算总金额与数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //4.遍历订单明细
        for (StockOutcomebillItem item : bill.getItems()) {
            //5.计算明细金额小计,并设置到明细中
            BigDecimal amount = item.getSalePrice().multiply(item.getNumber()).setScale(2, BigDecimal.ROUND_HALF_UP);
            //6.累加总金额与总数量
            item.setAmount(amount);
            //7.设置订单总金额和总数量
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
            //8.保存订单
            stockoutcomeebillMapper.insert(bill);

        }
            //9.为所有明细设置订单,并保存明细
            for (StockOutcomebillItem item : bill.getItems()) {
                item.setBillId(bill.getId());
                stockoutcomeebillItemMapper.insert(item);
            }



    }


    //具体的新增逻辑
    private void save (StockOutcomeBill bill){
        //1.设置录入相关信息
        bill.setInputUser(UserContext.getCurrentUser());
        bill.setInputTime(new Date());
        //1.1重置订单状态
        bill.setStatus(StockIncomeBill.STATUS_NORMAL);
        //2.计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //3.遍历明细,计算明细金额小计
        for (StockOutcomebillItem item : bill.getItems() ) {
            //4.累加总金额和总数量
            BigDecimal amount = item.getSalePrice().multiply(item.getNumber()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //5.设置订单总中数量
           totalAmount = totalAmount.add(amount);
           totalNumber = totalNumber.add(item.getNumber());
        }
        bill.setTotalAmount(totalAmount);
        bill.setTotalNumber(totalNumber);
        //6.保存订单
        stockoutcomeebillMapper.insert(bill);
        //7.跟新明细中的订单id保存明细
        for (StockOutcomebillItem item : bill.getItems()) {
            item.setBillId(bill.getId());
            stockoutcomeebillItemMapper.insert(item);
        }


    }





    public void delete(Long id) {
        StockOutcomeBill sib = stockoutcomeebillMapper.selectByPrimaryKey(id);
        if(sib.getStatus() == StockOutcomeBill.STATUS_AUDITED){

            throw  new LogicException("订单已经审核无法跟新");

        }
        if(id != null){
            stockoutcomeebillItemMapper.deleteByBillId(id);
            stockoutcomeebillMapper.deleteByPrimaryKey(id);
        }
    }

    public PageResult query(QueryObject qo) {
        int count = stockoutcomeebillMapper.queryForCount(qo);
        if(count == 0){
            return  PageResult.empty(qo.getPageSize());
        }
        List<?> list = stockoutcomeebillMapper.queryForList(qo);

        PageResult pageResult = new PageResult(qo.getPageSize(), count, qo.getCurrentPage(), list);


        return pageResult;
    }

    public void audit(Long id) {
        //先通过id查询当前订单
        StockOutcomeBill stockoutcomeebill = stockoutcomeebillMapper.selectByPrimaryKey(id);
        //判断订单是否审核
        if(stockoutcomeebill != null && StockOutcomeBill.STATUS_NORMAL == stockoutcomeebill.getStatus() ){
            //跟新审核相关信息
            stockoutcomeebill.setAuditor(UserContext.getCurrentUser());
            stockoutcomeebill.setAuditTime(new Date());
            stockoutcomeebill.setStatus(StockOutcomeBill.STATUS_AUDITED);
            //跟新订单
            stockoutcomeebillMapper.autid(stockoutcomeebill);

            //库存操作
            productStockService.stockOutcome(stockoutcomeebill);


            for (StockOutcomebillItem item : stockoutcomeebill.getItems()) {


        }
    }
    }

}

